<?php /* УТФ-8:fix */

class SPIcontroller__feedback__front extends SPIcontroller__feedback
{
	const FEEDBACKS_PER_PAGE = 5;

	public function exec( & $content )
	{
		$content['topic_template_filename'] = 'feedback/ask_form.tpl';
		$content['active_topic'] = $content['env_topic'];

		$content['linkway'][] = array(
			'name' => 'Обратная связь',
			'link' => SPIc::I('front')->link->standard('feedback')
		);

		//echo SPI::I('memcache')->gen_key(543);
		//---------------------------------------------------------------------

		if( SPI::I('var')->get('feedback_action') == 'savemessage' )
		{
			$this->savedata( $content ); # => die()
		}

		//---------------------------------------------------------------------

		//$askto_profile_id = (int) $content['env_vars']['i'];
		//$view_feedback_item_id = (int) $content['env_vars']['q'];
/*
		if( $view_feedback_item_id )
		{
			$content['details'] = SPIm::I('feedback')->items->front->get_by_id( $view_feedback_item_id );
			//$content['link_other_questions'] = SPIc::I('front')->link->feedback( $content['env_topic'], null, $askto_profile_id );
		}
		else
		{*/
			$page_index   = max( 1, (int) $content['env_vars']['page'] );
			$item_perpage = self::FEEDBACKS_PER_PAGE;
			$items_count  = SPIm::I('feedback')->items->front->select_count_after( );

			$select_offset = ($page_index-1) * $item_perpage;

			$content['last_items'] = array();

			$qres = SPIm::I('feedback')->items->front->select( $select_offset, $item_perpage );
//print_r($qres);
			$index_first = $items_count - ($page_index-1)*$item_perpage;
			foreach( $qres as $id )
			{
				$item = SPIm::I('feedback')->items->front->get_by_id( $id );
				$item['index'] = $index_first--;
				$content['last_items'][] = $item;
			}

			//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			// Pager

			$pages_count = ceil( $items_count / $item_perpage );

			if( $pages_count > 1 )
			{
				$content['last_items_pager'] = array();

				if( $page_index != 1 )
				{
					$content['last_items_pager']['prev']['link'] = SPIc::I('front')->link->feedback( $content['env_topic'], $page_index-1 );
				}

				if( $page_index != $pages_count )
				{
					$content['last_items_pager']['next']['link'] = SPIc::I('front')->link->feedback( $content['env_topic'], $page_index+1 );
				}

				$pages_indexes = SPIc::I('front')->utils->build_pages_indexes( $page_index, $pages_count );

				foreach( $pages_indexes as $page_i => $page_value )
				{
					if( $page_value == '..' )
					{
						$content['pager']['pages'][] = '..';
						continue;
					}

					$content['last_items_pager']['pages'][] = array(
						'index'     => $page_i,
						'link'      => SPIc::I('front')->link->feedback( $content['env_topic'], $page_i ),
						'is_active' => $page_i == $page_index,
					);
				}
			}
		//}


		//---------------------------------------------------------------------

		SPIc::I('captcha')->set( true );
		$content['captcha_image_source'] = SPIc::I('captcha')->get_image_source();

		//---------------------------------------------------------------------

		$content['link_submit'] = SPIc::I('front')->link->feedback( $content['env_topic'] );

		return true;
	}


	private function savedata( & $content )
	{
		$formdata = SPI::I('var')->get('feedback');

		//---------------------------------------------------------------------

		$errors = array();

		$in_name = trim( $formdata['name'] );
		if( $in_name == '' ) $errors['fb_name'] = 'Укажите Ваше имя.';

		$in_email = trim( $formdata['email'] );
		if( $in_email == '' ) $errors['fb_email'] = 'Укажите Ваше e-mail.';

		//---------------------------------------------------------------------
		
		$in_message = trim( $formdata['message'] );
		if( $in_message == '' ) $errors['fb_message'] = 'Напишите сообщение';

		
		//-----------------------------------------------------------------

		if( $formdata['captcha'] != SPIc::I('captcha')->get() )
		{
			SPIc::I('captcha')->reset();
			$errors['fb_captcha'] = 'Неверный код';
		}

		//-----------------------------------------------------------------

		if( count( $errors ) > 0 )
		{
			$response = array();
			$response['status'] = 'error';
			$response['error_messages'] = $errors; // as errors

			if( isset( $errors['fb_captcha'] ) === true )
			{
				$response['captcha_source'] = SPIc::I('captcha')->get_image_source();
			}
			else
			{
				$response['captcha_source'] = false;
			}

			SPIv::I('json')->render($response);
			die();
		}

		//---------------------------------------------------------------------

		$feedback_id = SPIm::I('feedback')->items->front->insert( $in_name, $in_email, $in_message );

		if( $feedback_id === false )
		{
			trigger_error( 'Возникла ошибка при сохранении данных', E_USER_ERROR );
			die();
		}

		$response = array();
		$response['status'] = 'done';
		SPIv::I('json')->render($response);
		die();
	}

	public function get_last_message()
	{
		$qres = SPIm::I('feedback')->items->front->select( 0, 1 );

		$item = SPIm::I('feedback')->items->front->get_by_id( $qres['id'] );

		if( $item === false )
		{
			return false;
		}
		$item['link'] = SPIc::I('front')->link->feedback( 'feedback' );

		return $item;
	}
}
